<template>
    <el-row class="edit">
        <el-container style="height: 100%;">
            <el-header height="60px">
                新增子部门
            </el-header>
            <el-main>
                <el-form ref="addDeptData" :rules="rules" :model="addDeptData" label-width="115px"
                         label-position="left">
                    <el-form-item label="部门名称 :" prop="name">
                        <el-input v-model="addDeptData.name" placeholder="请输入部门名称"></el-input>
                    </el-form-item>
                    <el-form-item label="部门代码 :" prop="code">
                        <el-input v-model="addDeptData.code" placeholder="请输入部门代码"></el-input>
                    </el-form-item>
                    <el-form-item label="是否是明细部门" prop="basic">
                        <el-radio-group v-model="addDeptData.basic">
                            <el-radio :label="true">是明细部门</el-radio>
                            <el-radio :label="false">不是明细部门</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="父级代码">
                        <el-row v-model="addDeptData.patCode">{{ addDeptData.patCode ? addDeptData.patCode : "无"}}
                        </el-row>
                    </el-form-item>
                    <el-form-item label="父级名称">
                        <el-row v-model="addDeptData.patName">{{ addDeptData.patName ? addDeptData.patName : "无"}}
                        </el-row>
                    </el-form-item>
                </el-form>
            </el-main>
            <el-footer height="80px">
                <el-row style="height: 20px;margin-bottom: 5px;">
                    <transition name="el-zoom-in-bottom">
                        <el-row v-show="infoShow" style="display: flex;justify-content: center;align-items: center;">
                            {{ (info !== '') ? "提示信息："+info : ''}}
                        </el-row>
                    </transition>
                </el-row>

                <el-row style="display: flex;justify-content: center;align-items: center;">
                    <el-button @click="submit" type="primary">
                        完成新增
                    </el-button>
                    <el-button @click="editOut">
                        取消新增
                    </el-button>
                </el-row>
            </el-footer>
        </el-container>
    </el-row>
</template>

<script>
    import {replaceSpacesObj} from '../../../utils/webUtils.js';

    export default {
        name: "addDept",
        data() {
            return {
                formAlignLeft: 'left',
                addDeptData: {
                    name: '',
                    code: '',
                    instUuid: this.dpInstUuid,
                    patUuid: this.deptUuid,
                    patName: '',
                    patCode: '',
                    basic: ''
                },
                rules: {
                    name: [
                        {required: true, message: '部门名称不得为空', trigger: 'blur'},
                    ],
                    code: [
                        {required: true, message: '部门代码不得为空', trigger: 'blur'},
                    ]
                },
                info: '',
                infoShow: false
            }
        },
        props: {
            dpInstUuid: {
                type: String
            },
            deptUuid: {
                type: String
            }
        },
        //深度监听父组件的props传值
        watch: {
            dpInstUuid(val) {
                this.addDeptData.instUuid = val;
            },
            deptUuid(val) {
                this.addDeptData.patUuid = val;
                this.queryDept();
            },
            other: {
                //深度观察对象
                handler(val) {
                },
                deep: true
            }
        },
        mounted() {
            this.queryDept();
        },
        methods: {
            queryDept() {
                this.axios.request({
                    method: "get",
                    url: "./kwp/kdi/dept/" + this.addDeptData.patUuid
                }).then(response => {
                    let patDept = response.data;
                    this.addDeptData.patCode = patDept.code;
                    this.addDeptData.patName = patDept.name;
                }).catch(error => {
                    console.log(error);
                });
            },
            submit() {
                this.$refs['addDeptData'].validate((valid) => {
                    if (!valid) {
                        return;
                    }
                    this.axios.request({
                        method: "post",
                        url: "./kwp/kdi/dept",
                        headers: {
                            "Content-Type": "application/json;charset=UTF-8"
                        },
                        data: replaceSpacesObj({
                            name: this.addDeptData.name,
                            code: this.addDeptData.code,
                            instUuid: this.addDeptData.instUuid,
                            patUuid: this.addDeptData.patUuid,
                            basic: this.addDeptData.basic
                        })
                    }).then(response => {
                        this.info = '新增成功,两秒后关闭';
                        this.infoShow = true;
                        setTimeout(() => {
                            this.infoShow = false;
                            this.$emit("editState", false);
                            this.cleanDept();
                            this.info = '';
                        }, 2000);
                    }).catch(error => {
                        console.log(error);
                        this.info = '新增失败';
                        this.infoShow = true;
                    });
                });
            },
            cleanDept() {
                this.addDeptData = {
                    name: '',
                    code: '',
                    instUuid: this.dpInstUuid,
                    patUuid: this.deptUuid,
                    patName: '',
                    patCode: '',
                    basic: ''
                };
            },
            editOut() {
                this.infoShow = false;
                this.cleanDept();
                this.info = '';
                this.$emit("editState", false);
            }

        }
    }
</script>

<style scoped>
    .edit {
        width: 400px;
        height: 100%;
        z-index: 100;
    }


</style>